/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#include "lyricsverticalpainter.h"
#include "configmanager.h"

static LyricsPainterManager::Register<LyricsVerticalPainter> painter("Vertical");

void LyricsVerticalPainter::drawLyrics(QPainter& painter, LyricsTime time) {
    // save old lyrics iterator to determine whether current lyrics changed
    static int pold = -1;

    if (m_drawState != DrawLyrics)
        return;

    const LyricsData& lyrics = *m_lyrics;

    // find current lyrics
    int pos = lyrics.find(time), py = pos;
    // if nothing found, exit
    if (pos == lyrics.size())
        return;

    ConfigManager* cfg = ConfigManager::getInstance();

    // determine where should i print the current line of lyrics
    int timeLength = (pos + 1 == lyrics.size()) ? 0 : lyrics[pos + 1].offset - lyrics[pos].offset;
    int spacing = cfg->value("Style/VerticalLyricsSpacing", 2).toInt();
    int lineHeight = m_fontMetrics->height() + spacing;//m_rect.height();
    int delayFactor = (timeLength == 0) ? 0 : (time - lyrics[pos].offset) * 0x100 / timeLength;
    int printY = m_rect.top() +
        m_rect.height() * cfg->value("Style/VerticalLyricsFactor", 0.5).toDouble() -
        lineHeight * delayFactor / 0x100;
    int printX;
    
    // current lyrics changed
    if (pold != pos) {
        pold = pos;
        updateInterval(lineHeight, timeLength);
    };

    // find the first line of lyrics i need to print 
    while (printY > m_rect.top() && pos > 0) {
        printY -= lineHeight;
        --pos;
    };

    // ok, now i print the lyrics line by line
    while (printY < (m_rect.top() + m_rect.height() + lineHeight) && pos < lyrics.size()) {
        printX = (m_rect.width() - m_fontMetrics->width(lyrics[pos].lyrics)) / 2;
        if (pos == py) {
            // print highlighted line
            drawText(painter, printX, printY,
                getFadingColor(time - lyrics[pos].offset, timeLength),
                lyrics[pos].lyrics);
        } else {
            drawText(painter, printX, printY,
                cfg->value("Style/FgColor", Qt::darkGreen).value<QColor>(),
                lyrics[pos].lyrics);
        };
        printY += lineHeight;
        ++pos;
    };
    //m_painter.drawLine(0, height() / 2, width(), height() / 2);
};
